package org.youyun56.gpsserver.command;

import java.io.IOException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;
import org.xml.sax.SAXException;
import org.youyun56.gpsserver.common.CollectionUtils;
import org.youyun56.gpsserver.common.FolderUtils;

public abstract class XMLConfigLoader {

    private static final Logger LOGGER = Logger.getLogger(XMLConfigLoader.class);

    protected String configFileRegExp = null;

    public XMLConfigLoader(String configFileRegExp) {
        this.configFileRegExp = configFileRegExp;
    }

    public String getConfigFileRegExp() {
        return configFileRegExp;
    }

    public void setConfigFileRegExp(String configFileRegExp) {
        this.configFileRegExp = configFileRegExp;
    }

    public void loadAllConfig(String folder) throws ParserConfigurationException, SAXException, IOException {
        List<String> files = FolderUtils.findFiles(folder, this.configFileRegExp);
        if (CollectionUtils.isNotEmpty(files)) {
            for (String file : files) {
                String f = folder + file;
                loadConfig(f);
            }
        }
    }

    public abstract void loadConfig(String file) throws ParserConfigurationException, SAXException, IOException;

}